﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Models;
using System.Data;
using EnterpriseFund.Common;
using Newtonsoft.Json;

namespace EnterpriseFund.Controllers
{
    public class DomesController : Controller
    {
        //
        // GET: /Domes/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Left()
        {
            Models.TreeModelList treeModelList = new Models.TreeModelList();

            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = new DataSet();
            if (OperateSession.get(GlobalConstant.PARAM_LOGINID) != "admin")
            {
                ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            }
            else
            {
                ds = prvFunctListBLL.GetList(" fvisible='Y'");
            }
            if (ds != null)
            {
                DataRow[] drmain = ds.Tables[0].Select("FFunctNo='m_EnterpriseFund'");

                if (drmain.Count() > 0)
                {
                    TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    {
                        Id = "Main",
                        Name = drmain[0]["FfunctName"].ToString(),
                        //ImageUrl = Url.Content(drmain[0]["FPictName"] == null ? "" : drmain[0]["FPictName"].ToString())
                    };
                    if (drmain[0]["FPictName"] != null && drmain[0]["FPictName"].ToString() != "")
                    {
                        treeParentItemModel.ImageUrl = Url.Content(drmain[0]["FPictName"].ToString());
                    }
                    DataRow[] drson = ds.Tables[0].Select("FPreFunctNo='" + drmain[0]["FFunctNo"].ToString() + "'");

                    foreach (DataRow dr in drson)
                    {
                        TreeItemModel treesonItemModel = new TreeItemModel(treeParentItemModel)
                        {
                            Id = dr["FfunctURL"].ToString() + "|" + dr["FfunctName"].ToString(),
                            Name = dr["FfunctName"].ToString(),
                            //ImageUrl = Url.Content(dr["FPictName"].ToString())
                        };
                        if (dr["FPictName"] != null && dr["FPictName"].ToString() != "")
                        {
                            treesonItemModel.ImageUrl = Url.Content(dr["FPictName"].ToString());
                        }
                        GetChildFuncList(dr, ds, treesonItemModel);
                    }
                    treeModelList.Add(treeParentItemModel);
                }
            }
            return View(treeModelList);
        }

        /// <summary>
        /// 递归获取树形菜单
        /// </summary>
        /// <param name="prvChildList">功能菜单列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="prvFunctListList">功能菜单集合</param>
        private void GetChildFuncList(DataRow dr, DataSet ds, TreeItemModel preTreeItemModel)
        {

            DataRow[] drson = ds.Tables[0].Select("FPreFunctNo='" + dr["FFunctNo"].ToString() + "'");

            foreach (DataRow drz in drson)
            {
                TreeItemModel treesonItemModel = new TreeItemModel(preTreeItemModel)
                {
                    Id = drz["FfunctURL"].ToString() + "|" + drz["FFunctNo"].ToString(),
                    Name = drz["FfunctName"].ToString(),
                    //ImageUrl = Url.Content(dr["FPictName"].ToString())
                };
                if (drz["FPictName"] != null && drz["FPictName"].ToString() != "")
                {
                    treesonItemModel.ImageUrl = Url.Content(drz["FPictName"].ToString());
                }
                GetChildFuncList(drz, ds, treesonItemModel);
            }

        }

        public ActionResult Content()
        {
            return View();
        }

        public string GetUrl(string FFunctNo)
        {
            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            Model.PrvFunctList pModel = prvFunctListBLL.GetModelByFunctNo(FFunctNo);
            if (pModel.FPictName != "")
            {
                pModel.FPictName = pModel.FPictName.Substring(1);
            }
            return JsonConvert.SerializeObject(pModel); ;
        }


        public ActionResult ContentShow()
        {
            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = new DataSet();
            if (OperateSession.get(GlobalConstant.PARAM_LOGINID) != "admin")
            {
                ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            }
            else
            {
                ds = prvFunctListBLL.GetList(" fvisible='Y'");
            }

            DataTable dt = ds.Tables[0];

            string str = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (str == "")
                {
                    str += dt.Rows[i]["FFunctNo"];
                }
                else
                {
                    str += "," + dt.Rows[i]["FFunctNo"];
                }
            }

            ViewData["NoList"] = str;

            return View();

        }

    }
}
